#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "configdialog.h"
#include <unistd.h>
#include <time.h>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QStringList labels;

    ui->setupUi(this);
    ctrl();

    connect(ctrl, SIGNAL(control_signal_print(QString, QString, QString, QString,
                        QString, QString)), this, SLOT(main_print(QString, QString,
                        QString, QString, QString, QString)));

    ui->dataTableWidget->setColumnCount(6);

    labels << "Status da Bomba" << "Semáforo da Bomba"
              << "Semáforo da Caixa 1" << "Semáforo da Caixa 2"
                 << "Nível da Caixa 1" << "Nível da Caixa 2";
    ui->dataTableWidget->setHorizontalHeaderLabels(labels);
    ui->dataTableWidget->horizontalHeader()->resizeSections(QHeaderView::Stretch);
    //view->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    ui->dataTableWidget->verticalHeader()->hide();
    ui->dataTableWidget->setShowGrid(false);
    ui->dataTableWidget->show();
}

MainWindow::~MainWindow()
{
    delete ctrl;
    delete ui;
}

/*void MainWindow::call_print(){
    emit main_control_print();
}*/

void MainWindow::on_startButton_clicked()
{
    //ctrl->Control_Main();
    //main_control();
     ctrl->start();
}

void MainWindow::on_pauseButton_clicked()
{

}

void MainWindow::on_stopButton_clicked()
{
    //pthread_exit(&wt1_clean);
    //pthread_exit(&wt2_clean);
    ctrl->Control_Stop();
}

void MainWindow::on_configButton_clicked()
{

    //crToolDlg dt;
    //dt.show();
    //dt.raise();
    //dt.exec();//for modal ->o caso!!

    ConfigDialog *conf = new ConfigDialog();
    //conf->configDialog_setCtrl(*ctrl);
    conf->raise();
    conf->exec();
    if(conf->confirmation){
        ctrl->Control_SetPumpOutput(conf->pumpOutput);
        ctrl->Control_SetTimeTap1(conf->timeToClearWt1);
        ctrl->Control_SetTimeTap2(conf->timeToClearWt2);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetArea(conf->areaWt1);
        ctrl->Control_GetWaterTank(2)->WaterTank_SetArea(conf->areaWt2);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetGaugeIn(conf->gaugeInWt1);
        ctrl->Control_GetWaterTank(2)->WaterTank_SetGaugeIn(conf->gaugeInWt2);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetGaugeOut(conf->gaugeOutWt1);
        ctrl->Control_GetWaterTank(2)->WaterTank_SetGaugeOut(conf->gaugeOutWt2);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetLevel(conf->inicialLevel1);
        ctrl->Control_GetWaterTank(2)->WaterTank_SetLevel(conf->inicialLevel2);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetHeight(conf->heightWt1);
        ctrl->Control_GetWaterTank(1)->WaterTank_SetHeight(conf->heightWt2);
    }
    delete conf;
}


void MainWindow::main_print(QString  pumpStatus, QString semPump, QString semWt1,
                QString semWt2, QString level1, QString level2){
    int row = ui->dataTableWidget->rowCount();

    ui->dataTableWidget->insertRow(row);
    ui->dataTableWidget->setItem(row, 0, new QTableWidgetItem(pumpStatus));
    ui->dataTableWidget->setItem(row, 1, new QTableWidgetItem(semPump));
    ui->dataTableWidget->setItem(row, 2, new QTableWidgetItem(semWt1));
    ui->dataTableWidget->setItem(row, 3, new QTableWidgetItem(semWt2));
    ui->dataTableWidget->setItem(row, 4, new QTableWidgetItem(level1));
    ui->dataTableWidget->setItem(row, 5, new QTableWidgetItem(level2));
    ui->dataTableWidget->show();

}

void MainWindow::print_wt(double level1, double level2){

    QPainter painter(this);

        QRectF rectangle(10.0, 20.0, 80.0, 60.0);
         int startAngle = 30 * 16;
         int spanAngle = 120 * 16;
         painter.drawArc(rectangle, startAngle, spanAngle);
}


/*void *MainWindow::main_control_print_helper(void *pMain){
    return ((MainWindow* )pMain)->main_control_print();
}*/

/*void *MainWindow::main_pump_control_helper(void *pControl)
{
    return ((Control* )pControl)->Control_Pump_Control();
}

void *MainWindow::main_wt1_clear_helper(void *pControl)
{
    return ((Control* )pControl)->Control_Wt1_Clear();
}

void *MainWindow::main_wt2_clear_helper(void *pControl)
{
    return ((Control* )pControl)->Control_Wt2_Clear();
}*/
